﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace tozhang.sdk
{
    internal class op
    {
    }
    
    public static class NetworkAPI
    {
        internal const string API_IP = "https://api.tozhang.cn/ip";
        internal const string METHOD_API = "GET";
        public static string ReadInternetIP()
        {
            try
            {
                // 创建HTTPS请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_IP);
                request.Method = METHOD_API;

                // 获取响应
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    // 读取响应内容
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (WebException ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
                // 处理网络异常
                if (ex.Response != null)
                {
                    using (WebResponse errorResponse = ex.Response)
                    {
                        using (Stream errorStream = errorResponse.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(errorStream))
                            {
                                string errorText = reader.ReadToEnd();
                                Console.WriteLine(errorText);
                            }
                        }
                    }
                }
#endif
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
            }

            return string.Empty;
        }

        public static string ReadInternetIPv6()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_IP);
                request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) => {
                    return new IPEndPoint(GetLocalAddress(IPAddress.Any, AddressFamily.InterNetworkV6), 0);
                };
                request.Method = METHOD_API;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
            }
            return string.Empty;

        }
        public static string ReadInternetIPv4()
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_IP);
                request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) => {
                    return new IPEndPoint(GetLocalAddress(IPAddress.Any, AddressFamily.InterNetwork), 0);
                };
                request.Method = METHOD_API;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
            }
            return string.Empty;
        }
        internal static IPAddress GetLocalAddress(IPAddress anyAddr, AddressFamily family)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress addr in hostEntry.AddressList)
            {
                if (addr.AddressFamily == family)
                {
                    return addr;
                }
            }
            throw new InvalidOperationException($"找不到本地{family}地址");
        }
    }
}
